<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>errorUtils</title>
</head>

<body>
    <div>
        打开浏览器控制台查看结果
    </div>
    <script>
        const errorUtils = (function () {
            let handleError = null;
            const res = {};
            res.registerErrorHandler = fn => {
                handleError = fn;
            };
            function callWithErrorHandling(fn) {
                try {
                    fn && fn();
                } catch (error) {
                    handleError(error);
                }
            }
            res.callWithErrorHandling = callWithErrorHandling;
            return res;
        })();
        errorUtils.registerErrorHandler(e => {
            console.warn(e);
        });
        errorUtils.callWithErrorHandling(() => {
            throw new Error('error!');
        });
    </script>
</body>

</html>